#include "Cube.h"

void Cube::intersection(const Rayon &r, Intersection & intersectRetour) {
	double tproche = DBL_MIN;
	double tloin = DBL_MAX;
	Vecteur normale;
	Vecteur normaleTmp;
	Vecteur directionRayon = r.getDirection();
	Point origineRayon = r.getOrigine();

	// plans selon X
	normaleTmp = Vecteur(-1,0,0);
	double xd = directionRayon.getX();
	double xo = origineRayon.getX();
	double xi = centre.getX()-demiArete;
	double xs = centre.getX()+demiArete;

	if (xd == 0) {
		if (xo < xi || xo > xs) {
			intersectRetour.setNull(true);
			return;
		}
	}

	double t1 = (xi - xo) / xd;
	double t2 = (xs - xo) / xd;

	if (t1 > t2) {
		double tmpt1 = t1;
		t1 = t2;
		t2 = tmpt1;
		normaleTmp = Vecteur(1,0,0);
	}
	if (t1 > tproche){
		tproche = t1;
		normale = normaleTmp;
	}
	if (t2 < tloin)	tloin = t2;
	
	if (tproche > tloin){
		intersectRetour.setNull(true);
		return;
	}
	if (tloin < 0){
		intersectRetour.setNull(true);
		return;
	}

	// plans selon Y
	normaleTmp = Vecteur(0,-1,0);
	double yd = directionRayon.getY();
	double yo = origineRayon.getY();
	double yi = centre.getY()-demiArete;
	double ys = centre.getY()+demiArete;

	if (yd == 0) {
		if (yo < yi || yo > ys) {
			intersectRetour.setNull(true);
			return;
		}
	}
	t1 = (yi - yo) / yd;
	t2 = (ys - yo) / yd;

	if (t1 > t2) {
		normaleTmp = Vecteur(0,1,0);
		double tmpt1 = t1;
		t1 = t2;
		t2 = tmpt1;
	}
	if (t1 > tproche){
		tproche = t1;
		normale = normaleTmp;
	}
	if (t2 < tloin)	tloin = t2;
	
	if (tproche > tloin){
		intersectRetour.setNull(true);
		return;
	}
	if (tloin < 0){
		intersectRetour.setNull(true);
		return;
	}
	// plans selon Z
	normaleTmp = Vecteur(0,0,-1);
	double zd = directionRayon.getZ();
	double zo = origineRayon.getZ();
	double zi = centre.getZ()-demiArete;
	double zs = centre.getZ()+demiArete;

	if (zd == 0) {
		if (zo < zi || zo > zs) {
			intersectRetour.setNull(true);
			return;
		}
	}
	t1 = (zi - zo) / zd;
	t2 = (zs - zo) / zd;

	if (t1 > t2) {
		normaleTmp = Vecteur(0,0,1);
		double tmpt1 = t1;
		t1 = t2;
		t2 = tmpt1;
	}
	if (t1 > tproche){
		tproche = t1;
		normale = normaleTmp;
	}
	if (t2 < tloin)	tloin = t2;
	
	if (tproche > tloin){
		intersectRetour.setNull(true);
		return;
	}
	if (tloin < 0){
		intersectRetour.setNull(true);
		return;
	}
	Point m = Point(xo + xd*tproche, yo + yd*tproche, zo + zd*tproche);
	intersectRetour.setPoint(m);
	intersectRetour.setDistance(tproche);
	intersectRetour.setNormale(normale);
	intersectRetour.setObjet(this);
	intersectRetour.setNull(false);
}
bool Cube::ombre(const Rayon &r, double distance) {
	double tproche = DBL_MIN;
	double tloin = DBL_MAX;
	Vecteur directionRayon = r.getDirection();
	Point origineRayon = r.getOrigine();

	// plans selon X
	double xd = directionRayon.getX();
	double xo = origineRayon.getX();
	double xi = centre.getX()-demiArete;
	double xs = centre.getX()+demiArete;

	if (xd == 0)
		if (xo < xi || xo > xs)
			return false;

	double t1 = (xi - xo) / xd;
	double t2 = (xs - xo) / xd;

	if (t1 > t2) {
		double tmpt1 = t1;
		t1 = t2;
		t2 = tmpt1;
	}
	if (t1 > tproche){
		tproche = t1;
	}
	if (t2 < tloin)	tloin = t2;
	
	if (tproche > tloin) return false;
	if (tloin < 0) return false;
	
	// plans selon Y
	double yd = directionRayon.getY();
	double yo = origineRayon.getY();
	double yi = centre.getY()-demiArete;
	double ys = centre.getY()+demiArete;

	if (yd == 0)
		if (yo < yi || yo > ys)
			return false;

	t1 = (yi - yo) / yd;
	t2 = (ys - yo) / yd;

	if (t1 > t2) {
		double tmpt1 = t1;
		t1 = t2;
		t2 = tmpt1;
	}
	if (t1 > tproche){
		tproche = t1;
	}
	if (t2 < tloin)	tloin = t2;
	
	if (tproche > tloin) return false;
	if (tloin < 0) return false;

	// plans selon Z
	double zd = directionRayon.getZ();
	double zo = origineRayon.getZ();
	double zi = centre.getZ()-demiArete;
	double zs = centre.getZ()+demiArete;

	if (zd == 0)
		if (zo < zi || zo > zs)
			return false;

	t1 = (zi - zo) / zd;
	t2 = (zs - zo) / zd;

	if (t1 > t2) {
		double tmpt1 = t1;
		t1 = t2;
		t2 = tmpt1;
	}
	if (t1 > tproche){
		tproche = t1;
	}
	if (t2 < tloin)	tloin = t2;
	
	if (tproche > tloin) return false;
	if (tloin < 0) return false;

	if (tproche > distance) return false;
	return true;
}